package com.lzx.lizhenxing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lzx.lizhenxing.common.constrants.CommonConstants;
import com.lzx.lizhenxing.common.exception.CommonException;
import com.lzx.lizhenxing.common.utils.UserUtils;
import com.lzx.lizhenxing.sys.dao.UserDao;
import com.lzx.lizhenxing.sys.model.User;
import com.lzx.lizhenxing.user.dao.ExpressDeliveryDao;
import com.lzx.lizhenxing.user.model.ExpressDeliveryModel;
import com.lzx.lizhenxing.user.param.ExpressDePageQueryParam;
import com.lzx.lizhenxing.user.service.ExpressDeliveryService;
import com.lzx.lizhenxing.user.vo.ExpressDeInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 快递表(ExpressDelivery)表服务实现类
 *
 */
@Service
public class ExpressDeliveryServiceImpl implements ExpressDeliveryService {

    @Resource
    private ExpressDeliveryDao expressDeliveryDao;

    @Resource
    private UserDao UserDao;

    @Override
    public ExpressDeInfoVo getExDeliveryInfo(ExpressDePageQueryParam param) {
        LambdaQueryWrapper<ExpressDeliveryModel> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isBlank(param.getBillNum())){
            queryWrapper.eq(ExpressDeliveryModel::getBillNum,param.getBillNum());
        }
        ExpressDeliveryModel expressDeModel = expressDeliveryDao.selectOne(queryWrapper);
        if(Objects.isNull(expressDeModel)){
            throw new CommonException(CommonConstants.ERROR,"请输入正确的快递单号");
        }
        // 判断该快递是不是本人快递，根据注册该系统的电话号判断
        Long uid = UserUtils.getUser();
        User user = UserDao.selectById(uid);
        if(!Objects.equals(user.getPhone(),expressDeModel.getPhone())){
            throw new CommonException(CommonConstants.ERROR,"不是本人快递不能导入");
        }
        ExpressDeInfoVo vo = new ExpressDeInfoVo();
        BeanUtils.copyProperties(expressDeModel,vo);
        return vo;
    }

    @Override
    public void setExDeliveryPayStatus() {

    }
}
